package service.imp;

import Dao.imp.ProductDao;
import Dao.imp.ProductNoteDao;
import Dao.imp.PurchasedProductDao;
import Dao.imp.UserDao;
import com.alibaba.fastjson.JSON;
import entity.*;
import service.IAdminService;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class AdminService implements IAdminService {
    private UserDao userDao = new UserDao();
    private ProductDao productDao = new ProductDao();
    private ProductNoteDao productNoteDao = new ProductNoteDao();
    private PurchasedProductDao purchasedProductDao = new PurchasedProductDao();
    @Override
    public String getAdminHome() {
        AdminHome obj = new AdminHome();
        obj.setUserCount(userDao.getUserCount());
        obj.setProductCount(productDao.getProductCount());
        obj.setProductNoteCount(productNoteDao.getProductNoteCount());
        obj.setUserList(userDao.queryAllUser().subList(0,3));
        obj.setProductList(productDao.queryAllProduct().subList(0,3));
        String date = JSON.toJSONString(obj);
        return date;
    }

    @Override
    public String getUser() {
        List<User> users = userDao.queryAllUser();
        String responseString = "";
        if (users != null) {
            responseString =  JSON.toJSONString(users);
        } else {
            Msg msg = new Msg(404, "get failed");
            responseString =  JSON.toJSONString(msg);
        }
        return responseString;
    }

    @Override
    public int doLogin(User admin) {
        /**
         * 1.根据username查询uid
         * 2.根据uid得到User
         * 3.从User中判断密码是否正确
         * 4.密码正确则返回正确uid,否则返回-1
         */
        int uid = userDao.queryUserUid(admin.getUsername());
        if (uid > 0) {
            User rs = userDao.queryUser(uid);
            if ("admin".equals(rs.getRealname()) && admin.getUsername().equals(rs.getUsername())
                    && admin.getPassword().equals(rs.getPassword())) {
                return uid;
            }
        }
        return -1;
    }

    @Override
    public String getUserByUid(int uid) {
        User user = userDao.queryUser(uid);
        String responseString = "";
        Msg msg = new Msg();
        if (user != null) {
            user.setPassword("");
            msg.setStatus(200);
            msg.setData(user);
        } else {
            msg.setStatus(404);
            msg.setData("get failed");
        }
        return JSON.toJSONString(msg);
    }

    @Override
    public String updateUserInfo(User user) {
        int rs = userDao.updateUser(user);
        Msg msg = new Msg();
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        return JSON.toJSONString(msg);
    }

    @Override
    public String deleteUser(int uid) {
        String responseString = "";
        int rs = userDao.deleteUser(uid);
        Msg msg = new Msg();
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String doRegister(User user) {
        String responseString = "";
        user.setModifytime(new Date());
        int rs = userDao.addUser(user);
        Msg msg = new Msg();
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String getProduct() {
        List<Product> products = productDao.queryAllProduct();
        String responseString = "";
        if (products != null) {
            responseString =  JSON.toJSONString(products);
        } else {
            Msg msg = new Msg(404, "get failed");
            responseString =  JSON.toJSONString(msg);
        }
        return responseString;
    }

    @Override
    public String updateProduct(Product product) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = productDao.updateProduct(product);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String deleteProduct(int pid) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = productDao.deleteProduct(pid);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String addProduct(Product product) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = productDao.addProduct(product);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String getProductnote() {

        List<ProductNote> productNotes = productNoteDao.queryAllproductNote();
        String responseString = "";
        if (productNotes != null) {
            //利用第三方插件将bean转换成json文件,然后返回到servlet层中
            responseString = JSON.toJSONString(productNotes);
        } else {
            Msg msg = new Msg(404, "get failed");
            responseString = JSON.toJSONString(msg);
        }
        return responseString;
    }

    @Override
    public String deleteProductnote(int pid) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = productNoteDao.deleteProductNote(pid);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String addProductnote(ProductNote productNote) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = productNoteDao.addProductNote(productNote);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String updateProductnote(ProductNote productNote) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = productNoteDao.updateProductNote(productNote);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String getPurchasedProduct() {
        List<PurchasedProduct> PurchasedProducts = purchasedProductDao.queryAllPurchasedProduct();
        String responseString = "";
        if (PurchasedProducts != null) {
            responseString =  JSON.toJSONString(PurchasedProducts);
        } else {
            Msg msg = new Msg(404, "get failed");
            responseString =  JSON.toJSONString(msg);
        }
        return responseString;
    }

    @Override
    public String deletePurchaseProduct(int ppid) {
        String responseString = "";
        int rs = purchasedProductDao.deletePurchasedProduct(ppid);
        Msg msg = new Msg();
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }

    @Override
    public String addPurchaseProduct(PurchasedProduct purchasedProduct) {
        String responseString = "";
        Msg msg = new Msg();
        int rs = purchasedProductDao.addPurchasedProduct(purchasedProduct);
        if (rs > 0) {
            msg.setStatus(200);
        } else {
            msg.setStatus(404);
        }
        responseString = JSON.toJSONString(msg);
        return responseString;
    }


}
